﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContexts.Supplier;

public partial class Supplier_Location_Timings : System.Web.UI.Page
{
    private SupplierDataContext supplierContext = new SupplierDataContext();

    private int SupplierLocationId
    {
        get { return Helper.QSIntValue("id"); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (SupplierLocationId > 0)
            {
                var locationTimes = from slt in supplierContext.SupplierLocationTimes
                                    where slt.SupplierLocationId == SupplierLocationId
                                    orderby slt.Day
                                    select slt;

                int iDay = 1;
                foreach (var locTimes in locationTimes)
                {
                    switch (iDay)
                    {
                        //Sunday
                        case 1:
                            txtSundayStart.Text = locTimes.StartTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            txtSundayEnd.Text = locTimes.EndTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            break;
                        //Monday
                        case 2:
                            txtMondayStart.Text = locTimes.StartTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            txtMondayEnd.Text = locTimes.EndTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            break;
                        //Tuesday
                        case 3:
                            txtTuesdayStart.Text = locTimes.StartTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            txtTuesdayEnd.Text = locTimes.EndTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            break;
                        //Wednesday
                        case 4:
                            txtWednesdayStart.Text = locTimes.StartTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            txtWednesdayEnd.Text = locTimes.EndTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            break;
                        //Thursday
                        case 5:
                            txtThursdayStart.Text = locTimes.StartTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            txtThursdayEnd.Text = locTimes.EndTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            break;
                        //Friday
                        case 6:
                            txtFridayStart.Text = locTimes.StartTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            txtFridayEnd.Text = locTimes.EndTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            break;
                        //Saturday
                        case 7:
                            txtSaturdayStart.Text = locTimes.StartTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            txtSaturdayEnd.Text = locTimes.EndTime.Value.ToString(Constants.DECIMAL_FORMAT);
                            break;
                    };
                    iDay++;
                }
            }
            else
            {
                Response.Redirect("default.aspx");
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        var locationTimes = from slt in supplierContext.SupplierLocationTimes
                            where slt.SupplierLocationId == SupplierLocationId
                            select slt;

        SupplierLocation suppLocation = supplierContext.SupplierLocations.SingleOrDefault(p => p.SupplierLocationId == SupplierLocationId);
        supplierContext.SupplierLocationTimes.DeleteAllOnSubmit(locationTimes);

        supplierContext.SupplierLocationTimes.InsertOnSubmit(new SupplierLocationTime { SupplierLocationId = SupplierLocationId, StartTime = Convert.ToDecimal(txtSundayStart.Text), EndTime = Convert.ToDecimal(txtSundayEnd.Text), Day = 1, SupplierId = suppLocation.SupplierId, LocationId = suppLocation.LocationId });

        supplierContext.SupplierLocationTimes.InsertOnSubmit(new SupplierLocationTime { SupplierLocationId = SupplierLocationId, StartTime = Convert.ToDecimal(txtMondayStart.Text), EndTime = Convert.ToDecimal(txtMondayEnd.Text), Day = 2, SupplierId = suppLocation.SupplierId, LocationId = suppLocation.LocationId });

        supplierContext.SupplierLocationTimes.InsertOnSubmit(new SupplierLocationTime { SupplierLocationId = SupplierLocationId, StartTime = Convert.ToDecimal(txtTuesdayStart.Text), EndTime = Convert.ToDecimal(txtTuesdayEnd.Text), Day = 3, SupplierId = suppLocation.SupplierId, LocationId = suppLocation.LocationId });

        supplierContext.SupplierLocationTimes.InsertOnSubmit(new SupplierLocationTime { SupplierLocationId = SupplierLocationId, StartTime = Convert.ToDecimal(txtWednesdayStart.Text), EndTime = Convert.ToDecimal(txtWednesdayEnd.Text), Day = 4, SupplierId = suppLocation.SupplierId, LocationId = suppLocation.LocationId });

        supplierContext.SupplierLocationTimes.InsertOnSubmit(new SupplierLocationTime { SupplierLocationId = SupplierLocationId, StartTime = Convert.ToDecimal(txtThursdayStart.Text), EndTime = Convert.ToDecimal(txtThursdayEnd.Text), Day = 5, SupplierId = suppLocation.SupplierId, LocationId = suppLocation.LocationId });

        supplierContext.SupplierLocationTimes.InsertOnSubmit(new SupplierLocationTime { SupplierLocationId = SupplierLocationId, StartTime = Convert.ToDecimal(txtFridayStart.Text), EndTime = Convert.ToDecimal(txtFridayEnd.Text), Day = 6, SupplierId = suppLocation.SupplierId, LocationId = suppLocation.LocationId });

        supplierContext.SupplierLocationTimes.InsertOnSubmit(new SupplierLocationTime { SupplierLocationId = SupplierLocationId, StartTime = Convert.ToDecimal(txtSaturdayStart.Text), EndTime = Convert.ToDecimal(txtSaturdayEnd.Text), Day = 7, SupplierId = suppLocation.SupplierId, LocationId = suppLocation.LocationId });

        supplierContext.SubmitChanges();

        Response.Redirect("default.aspx");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx");
    }
}
